package xyz.yuanjilu.service.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import xyz.yuanjilu.base.Constant;
import xyz.yuanjilu.dao.WebpageinfoDao;
import xyz.yuanjilu.entity.WebpageinfoEntity;
import xyz.yuanjilu.service.SplitWordService;
import xyz.yuanjilu.service.WebpageinfoService;
import xyz.yuanjilu.utils.Encrypt;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;



@Service("webpageinfoService")
public class WebpageinfoServiceImpl implements WebpageinfoService,Pipeline {
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private WebpageinfoDao webpageinfoDao;
	@Autowired
	private SplitWordService splitWordService;
	
	@Resource(name="webpageinfoCache")
	private Cache cache;
	@Override
	public WebpageinfoEntity queryObject(String id){
		Element element = cache.get(id);
		WebpageinfoEntity queryObject=null;
		if (element==null) {
			queryObject = webpageinfoDao.queryObject(id);
			if (queryObject!=null) {
				logger.info(id + "加入到缓存： " + cache.getName());
				element = new Element(id,queryObject);
	        	cache.put(element);
			}
		}else{
			queryObject=(WebpageinfoEntity) element.getObjectValue();
			logger.info(id + "使用缓存： " + cache.getName());
		}
		
		return queryObject;
	}
	
	@Override
	public List<WebpageinfoEntity> queryList(Map<String, Object> map){
		return webpageinfoDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return webpageinfoDao.queryTotal(map);
	}
	
	@Override
	public void save(WebpageinfoEntity webpageinfo){
		webpageinfoDao.save(webpageinfo);
	}
	
	@Override
	public void update(WebpageinfoEntity webpageinfo){
		webpageinfoDao.update(webpageinfo);
	}
	
	@Override
	public void delete(String id){
		webpageinfoDao.delete(id);
	}
	
	@Override
	public void deleteBatch(String[] ids){
		webpageinfoDao.deleteBatch(ids);
	}
	@Override
	public List<WebpageinfoEntity> queryNoPushWebPageList(Map<String, Object> map){
		return webpageinfoDao.queryNoPushWebPageList(map);
	}

    @Override
    public void process(ResultItems resultItems, Task task) {
        WebpageinfoEntity webpageinfoEntity=new WebpageinfoEntity();
        Object object = resultItems.get(Constant.ANT_BEAN_KEY_URL);
        if (object==null) {
            return;
        }
        String id = Encrypt.md5(object+"");
        WebpageinfoEntity queryObject = this.queryObject(id);
        if (queryObject==null) {
            webpageinfoEntity.setId(id);
            webpageinfoEntity.setAntBeanId(resultItems.get(Constant.ANT_BEAN_KEY_ID)+"");
            webpageinfoEntity.setUrl(resultItems.get(Constant.ANT_BEAN_KEY_URL)+"");
            webpageinfoEntity.setTitle(resultItems.get(Constant.ANT_BEAN_KEY_TITLE)+"");
            webpageinfoEntity.setContent(resultItems.get(Constant.ANT_BEAN_KEY_CONTENT)+"");
            webpageinfoEntity.setDate(resultItems.get(Constant.ANT_BEAN_KEY_DATE)+"");
            webpageinfoEntity.setAuthor(resultItems.get(Constant.ANT_BEAN_KEY_AUTHOR)+"");
            webpageinfoEntity.setCreateDate(new Date());
            webpageinfoDao.save(webpageinfoEntity);
        }
        /*List<String> highWords = splitWordService.getHighWords(resultItems.get("content")+"");
        for (String string : highWords) {
            //System.out.println(string);
        }*/
        
    }
}
